﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework.Common;
using FantastPlatform.Framework.Exception;
using Newtonsoft.Json;
using FantastPlatform.Framework.Common.Serializable;

namespace FantastPlatform.Framework.Model
{
    public class User :Item,IUser
    {
        #region IUser 成员

        public virtual string Password{get;set;}

        public virtual IList<IRole> Roles { get; set; }

        private IList<IEmployee> employees;
        private IList<IEmployee> Employees
        {
            get
            {
                if (CollectionHelper.IsNullOrEmpty<IEmployee>(employees))
                    employees = new List<IEmployee>();
                return employees;
            }
            set
            {
                employees = value;
            }
        }
        [JsonConverter(typeof(InterfaceConverter<Employee>))]
        public virtual IEmployee Employee
        {
            get
            {
                Check.Require<InvalidLengthException>(Employees.Count<=1, string.Format("用户：{0}不能对应多个员工！",this.Name));
                return CollectionHelper.IsNullOrEmpty<IEmployee>(Employees) ? null : Employees.SingleOrDefault();
            }
            set
            {
                Employees.Clear();
                Employees.Add(value);
            }
        }

        #endregion
    }
}
